package com.lzx.kafka.controller;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName DingController
 * @Author 刘正星
 * @Date 2021/4/26 下午6:49
 * @Description
 */
@Controller
@RequestMapping("/dingding")
public class DingController {
    @Value("${dewu.notify.dingding}")
    private String dingdingUrl;

    @RequestMapping("/text")
    @ResponseBody
    public String sendText(){
        System.out.println(this.dingdingUrl);
        // #0. 配置钉钉客户端，dingdingUrl即根据刚才拷贝的Webhook
        DingTalkClient client = new DefaultDingTalkClient(this.dingdingUrl);
        // #1. request表示整个消息请求
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        // #2. 请求设置消息类别
        request.setMsgtype("markdown");

        // #3. 设置消息内容

//        OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
//        text.setContent("小订得物来新订单了");
//
//        OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
//        link.setTitle("小订来新订单啦");
//        link.setText("小订得物App是全球领先的集正品潮流装备、潮流商品鉴别、潮流生活社区于一体的新一代潮流网购社区");
//        link.setMessageUrl("https://www.dewu.com/about");
//        link.setPicUrl("https://china-product.poizon.com/FuykjexQ6q_23EBTg7aMVefZMBX1new.png");
//        request.setLink(link);

        OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
        markdown.setTitle("小订来新订单啦");
        markdown.setText( "#### 得物来新订单了\n" +
                ">刘正星 购买了 Nike Tanjun 男子 休闲跑鞋" +
                "> ![screenshot](https://china-product.poizon.com/FuykjexQ6q_23EBTg7aMVefZMBX1new.png)\n"  +
                "> 支付中");

        request.setMarkdown(markdown);
        // #4. 设置钉钉@功能
        OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
        at.setIsAtAll(true);
        request.setAt(at);
        try {
            // #5. 发送消息请求
            client.execute(request);

        } catch (ApiException e) {
            e.printStackTrace();
        }
        return "success";
    }
}
